package com.hspedu.reflection.class_;

import com.hspedu.Car;

import java.lang.reflect.Field;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/22
 * @time 17:54
 */
public class Class02 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        String classAllPath = "com.hspedu.Car";
        Class cls = Class.forName(classAllPath);
        //显示 cls 是哪个类的对象
        System.out.println("cls = " + cls);
        //显示 cls 的运行类型
        System.out.println(cls.getClass());
        System.out.println("cls.getPackage().getName() = " + cls.getPackage().getName());
        System.out.println("cls.getName() = " + cls.getName());
        Car car = (Car) cls.newInstance();
        System.out.println("car = " + car);
        Field name = cls.getField("name");
        System.out.println(name.get(car));
        name.set(car,"benz");
        System.out.println(name.get(car));
        System.out.println("==================");
        Field[] fields = cls.getFields();
        for (Field field : fields) {
            System.out.println(  field.get(car));
        }
    }
}
